<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@include file="/WEB-INF/views/include/treeview.jsp" %>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）"%>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）"%>
<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框"%>
<%@ attribute name="selectIds" type="java.lang.String" required="false" description="默认选中的值"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）"%>
<%@ attribute name="parentId" type="java.lang.String" required="false" description="选择父ID"%>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允许选择根节点"%>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允许选择父节点"%>
<%@ attribute name="module" type="java.lang.String" required="false" description="过滤栏目模型（只显示指定模型，仅针对CMS的Category树）"%>
<%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="选择范围内的模型（控制不能选择公共模型，不能选择本栏目外的模型）（仅针对CMS的Category树）"%>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制选择，如果限制，设置为disabled"%>
<div class="input-append">
	<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
	<input id="${id}Name" name="${labelName}" readonly="readonly" type="text" value="${labelValue}" maxlength="50"
		class="${cssClass}" style="${cssStyle}"/><a id="${id}Button" href="javascript:" class="btn ${disabled} btn-primary" style="line-height: 34px;height: 34px;padding: 1px 3px 1px 3px;">&nbsp;<i class="fa fa-search"></i>&nbsp;</a>&nbsp;&nbsp;
</div>
<div class="modal fade" id="treeModal">
	<div class="modal-dialog" style="width: 400px;">
		<div class="modal-content">
			<div class="modal-header" style="height: 40px;padding: 5px;">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close" style="font-size: 20px; "><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">请选择${title}</h4>
			</div>
			<div class="modal-body" style="padding: 4px; ">
				<ul id="tree" class="ztree"></ul>
			</div>
			<div class="modal-footer" style="padding-top: 5px;padding-bottom: 5px; ">
				<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-warning btn-sm" id="${id}clearTreeBtn">清除</button>
				<button type="button" class="btn btn-primary btn-sm" id="${id}btnSureTree">选择</button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	var tree;
	var setting = {
			view:{selectedMulti:false},
			check:{
				enable:"${checked}",
				nocheckInherit:true
			},
			data:{
				simpleData:{enable:true}
			},
			view:{
				fontCss:function(treeId, treeNode) {
					return (!!treeNode.highlight) ? {"font-weight":"bold"} : {"font-weight":"normal"};
				}
			},
			callback:{
				beforeClick:function(id, node){
					if("${checked}" == "true"){
						tree.checkNode(node, !node.checked, true, true);
						return false;
					}
				},
				onClick:function(){
					$("#testSpan").hide();
				},
				onDblClick:function(event,treeId,treeNode){
					$("#btnSureTree").trigger("click");
				}
			},
			onDblClick:function(){
				alert(1);
			}

	};

	$(document).ready(function(){
		$("#${id}Button").click(function(){
			$("#treeModal").modal({backdrop:"static"});
			var parentId = $("#${parentId}").val();
			$.get("${ctx}${url}${fn:indexOf(url,'?')==-1?'?':'&'}&extId=${extId}&parentId="+parentId+"&module=${module}&t="+new Date().getTime(), function(zNodes){
				// 初始化树结构
				tree = $.fn.zTree.init($("#tree"), setting, zNodes);

				// 默认展开一级节点
				var nodes = tree.getNodesByParam("level", 0);
				for(var i=0; i<nodes.length; i++) {
					tree.expandNode(nodes[i], true, false, false);
				}
				// 默认选择节点
				var ids = "${selectIds}".split(",");
				for(var i=0; i<ids.length; i++) {
					var node = tree.getNodeByParam("id", ids[i]);
					if("${checked}" == "true"){
						try{tree.checkNode(node, true, true);}catch(e){}
						tree.selectNode(node, false);
					}else{
						tree.selectNode(node, true);
					}
				}
			});
		});

		//确定选择
		$("#${id}btnSureTree").click(function(){
			var ids = [], names = [], nodes = [];
			if ("${checked}" == "true"){
				nodes = tree.getCheckedNodes(true);
			}else{
				nodes = tree.getSelectedNodes();
			}
			for(var i=0; i<nodes.length; i++) {//<c:if test="${checked}">
				if (nodes[i].isParent){
					continue; // 如果为复选框选择，则过滤掉父节点
				}//</c:if><c:if test="${notAllowSelectRoot}">
				if (nodes[i].level == 0){
					alert("不能选择根节点（"+nodes[i].name+"）请重新选择。");
					return false;
				}//</c:if><c:if test="${notAllowSelectParent}">
				if (nodes[i].isParent){
					alert("不能选择父节点（"+nodes[i].name+"）请重新选择。");
					return false;
				}//</c:if><c:if test="${not empty module && selectScopeModule}">
				if (nodes[i].module == ""){
					alert("不能选择公共模型（"+nodes[i].name+"）请重新选择。");
					return false;
				}else if (nodes[i].module != "${module}"){
					alert("不能选择当前栏目以外的栏目模型，请重新选择。");
					return false;
				}//</c:if>
				ids.push(nodes[i].id);
				names.push(nodes[i].name);//<c:if test="${!checked}">
				break; // 如果为非复选框选择，则返回第一个选择  </c:if>
			}
			$("#${id}Id").val(ids);
			$("#${id}Name").val(names);
			$("#treeModal").modal("hide");
			return false;
		});

		//清除所有已经选择的节点
		$("#${id}clearTreeBtn").click(function(){
			$("#${id}Id").val("");
			$("#${id}Name").val("");
			$("#treeModal").modal("hide");
		});
	});
</script>